package com.example.newspublish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.newspublish.exception.NewsServiceException;
import com.example.newspublish.mapper.NewsMapper;
import com.example.newspublish.pojo.dto.NewsPageDTO;
import com.example.newspublish.pojo.entity.Comment;
import com.example.newspublish.pojo.entity.News;
import com.example.newspublish.pojo.form.NewsForm;
import com.example.newspublish.pojo.result.ResultCodeEnum;
import com.example.newspublish.pojo.vo.NewsPageVO;
import com.example.newspublish.service.NewsService;
import com.example.newspublish.util.result.PageResult;
import com.example.newspublish.util.result.Result;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    NewsMapper newsMapper;

    @Override
    public Result getNewsListById(Long categoryId) {
        List<News> newsList = newsMapper.getNewsListById(categoryId);
        return Result.success("获取成功!", newsList);
    }

    @Override
    public Result getNewsList() {
        List<News> newsList = newsMapper.getNewsList();
        return Result.success("获取成功!", newsList);
    }

    @Override
    public Result getNewDetailById(Long id) {
        News newsItem = newsMapper.getNewDetailById(id);
        return Result.success("获取成功!", newsItem);
    }

    @Override
    public Result addNew(NewsForm newsForm) {
        News newsItem = new News();
        BeanUtils.copyProperties(newsForm, newsItem);
//        newsItem.setPublishTime(new Timestamp(System.currentTimeMillis()));
        if(!ObjectUtils.isNotEmpty(newsItem.getCategoryId())) {
            throw new NewsServiceException(ResultCodeEnum.FAIL, "新闻分类id不能为空");
        }
        int rows = newsMapper.addNew(newsItem);
        if(rows == 1) {
            return Result.success("发布新闻成功~",null);
        }
        else {
            return Result.failed(20001,"添加失败~");
        }
    }

    @Override
    public Result deleteNewById(Long id) {

        News news = newsMapper.selectById(id);
        if(!ObjectUtils.isNotEmpty(news)) {
            throw new NewsServiceException(ResultCodeEnum.FAIL, "新闻分类id不能为空");
        }
        int rows = newsMapper.deleteNewById(id);
        if(rows == 1) {
            return Result.success("删除成功新闻~",null);
        }
        else {
            return Result.failed(20001,"删除失败~");
        }
    }

    /**
     * 搜索新闻
     * @param key
     * @return
     */
    @Override
    public List<News> search(String key) {
        return newsMapper.search(key);
    }

    public Page<News> pageQuery(int pageNum, int pageSize) {

        Page<News> newsByPage = new Page<>(pageNum, pageSize);
        return newsMapper.selectPage(newsByPage,null);
    }

    /**
     * 根据用户名获取该用户发布新闻
     * @param author
     * @return
     */
    @Override
    public List<News> getNewsByUserName(String author) {
        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(News::getAuthor, author);
        List<News> news = newsMapper.selectList(queryWrapper);
        if(news == null) {
            throw new NewsServiceException(ResultCodeEnum.AUTHEN_ERROR,"获取失败");
        }
        return news;
    }
}
